<?php
namespace App\Modules\Relationship\Models;


use App\Http\Services\CommonServices;
use App\Modules\Relationship\Interfaces\ModelInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Base;

class CoupleModel extends Base implements ModelInterface {

    protected $table = "coupling";
    protected $fields = ['id', 'user_id as userId', 'cp_user_id as cpUserId',
        'intimate', 'level', 'autograph', 'ring_id as ringId',
        'ring_record_id as ringRecordId','keepsake_id as keepsakeId','create_time as createTime',
        'create_timestamp as createTimestamp',
        'update_time as updateTime',
        'is_deleted as isDeleted'
        ];

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    // 获取某条用户关系记录
    public function getRecord(int $userId, int $cpUserId){

        $condition = array(
            "user_id"=>$userId,
            "cp_user_id"=>$cpUserId,
            "is_deleted"=>0
        );
        $return = $this->getOneByCondition($condition, $this->fields);
        return $return;
    }

    // 查询一组记录
    public function getGroupRecord ($userId, $friendId) {
        $re1 = $this->getRecord($userId, $friendId);
        $re2 = $this->getRecord($friendId, $userId);
        if (!$re1 || !$re2) {
            return null;
        }
        return [$re1, $re2];
    }

    // 查询一组记录
    public function getRecordByUserId ($userId) {
        return $this->getOneByCondition(array('user_id'=>$userId, 'is_deleted'=>0), $this->fields);
    }

    public function getone($limit) {
        $fields = implode(',', $this->fields);
        $sql = "SELECT {$fields} FROM zb_coupling limit {$limit}, 1";
        $record = DB::select($sql);
        return self::objToArray($record[0]);
    }

    public function updateone($id, $data) {
        DB::table($this->table)->where(array('id'=>$id))->update($data);
    }

    public function createRecord (array $data){
        $data['create_timestamp'] = time();
        $data['update_time'] = date('Y-m-d H:i:s', time());
        return DB::table($this->table)->insertGetId($data);
    }

    public function updateRecord (array $ids, array $data){
        return DB::table($this->table)->whereIn('id', $ids)->update($data);
    }

    public function deleteRecord (array $ids) {
        return DB::table($this->table)->whereIn('id', $ids)->delete();
    }

    public function addIntimate (int $id, int $intimate) {
        return DB::table($this->table)->where('id', $id)->increment('intimate', $intimate);
    }

    // 获取用户关系分表和关联信息表
    public function getRecordAndAttr (string $rsCode){

    }

    // 修改戒指
    public function updateRing ($id, $ringId) {
        $condition = array(
            'id'=>$id
        );
        $data = array(
            'ring_id'=>$ringId
        );
        $this->updateByCondition($condition, $data);
    }

    public function incIntimate1($userId, $friendId, $intimate, $level) {
        $table = $this->tablePrefix . $this->table;
        $record = $this->getRecord($userId, $friendId);
        if ($record == null) {

        }
        $sql1 = "UPDATE {$table} SET level = {$level}, intimate = intimate + {$intimate} WHERE id = {$record[0]['id']}";
        $sql2 = "UPDATE {$table} SET level = {$level}, intimate = intimate + {$intimate} WHERE id = {$record[1]['id']}";
        $rs1 = DB::update($sql1);
        $rs2 = DB::update($sql2);
        if (!$rs1 || !$rs2) {
            return null;
        }
        return true;
    }

    // 增加分值
    public function incIntimate($userId, $friendId, $intimate, $level) {
         $record = $this->getGroupRecord($userId, $friendId);
         if (!$record) {
             return null;
         }

    }
}

